﻿using System;
using System.Threading;
using AccelerateSensor.Client.Controls;
using AccelerateSensor.Client.Tools;
using AccelerateSensor.Service;
using ZbSkin.Frm;

namespace AccelerateSensor.Client
{
    public partial class FormMain : SkinMainForm
    {
        private readonly SynchronizationContext _context;
        private readonly BottomControl _bottomControl;
        private readonly PageControl _gatewayControl;
        private readonly PageControl _nodeControl;
        private readonly PageControl _nodeHistoryDataControl;
        private PageControl _curControl;

        public FormMain()
        {
            InitializeComponent();

            _context = SynchronizationContext.Current;

            _bottomControl = new BottomControl();
            BottomPanel.Controls.Add(_bottomControl);

            _gatewayControl = new GatewayControl() {Visible = false, Width = Constants.ContentWidth};
            _nodeControl = new NodeControl() {Visible = false, Width = Constants.ContentWidth};
            _nodeHistoryDataControl = new NodeHistoryDataControl() { Visible = false, Width = Constants.ContentWidth };
            ContentPanel.Controls.Add(_gatewayControl);
            ContentPanel.Controls.Add(_nodeControl);
            ContentPanel.Controls.Add(_nodeHistoryDataControl);

            BottomPanel.SizeChanged += OnBottomPanelSizeChanged;
            ContentPanel.SizeChanged += OnContentPanelSizeChanged;

            _gatewayControl.SwitchPage += OnGatewaySwitchPage;
            _nodeControl.SwitchPage += OnNodeSwitchPage;
            _nodeHistoryDataControl.SwitchPage += OnNodeHistoryDataSwitchPage;

            Width = Constants.FormWidth;
            Height = Constants.FormHeight;

            LoadMax = true;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            Title = "威迈无线传感器";
            SetCurControl(_gatewayControl);

            //启动服务
            AccelerateSensorService.Instance.StartFailedEvent += OnStartFailed;
            AccelerateSensorService.Instance.Startup();
        }

        private void OnStartFailed()
        {
            _context?.Post((state) =>
            {
                MessageBoxEx.Warning("启动Socket服务器失败！");
                ForceClose();
            }, null);
        }

        protected override void DoFormClosed()
        {
            //关闭Socket服务器
            AccelerateSensorService.Instance.Shutdown();

            base.DoFormClosed();
        }

        private void OnContentPanelSizeChanged(object sender, EventArgs e)
        {
            foreach (PageControl page in ContentPanel.Controls)
            {
                if (page != null)
                {
                    page.Height = ContentPanel.ClientSize.Height - 20;
                    page.Left = (ContentPanel.ClientSize.Width - page.Width) / 2;
                    page.Top = 10;
                }
            }
        }

        private void OnBottomPanelSizeChanged(object sender, EventArgs e)
        {
            _bottomControl.Width = BottomPanel.Width;
            _bottomControl.Height = BottomPanel.Height;
            _bottomControl.Left = 0;
            _bottomControl.Top = 0;
        }

        private void SetCurControl(PageControl control, object data = null)
        {
            control.ShowControl(true, data);
            _curControl?.ShowControl(false, null);
            _curControl = control;

            control.Width = Constants.ContentWidth;
            control.Height = ContentPanel.ClientSize.Height - 20;
            control.Left = (ContentPanel.ClientSize.Width - control.Width) / 2;
            control.Top = 10;
        }

        private void OnGatewaySwitchPage(Tuple<string, object> data)
        {
            SetCurControl(_nodeControl, data?.Item2);
        }

        private void OnNodeSwitchPage(Tuple<string, object> data)
        {
            switch (data?.Item1)
            {
                case Constants.PageTag.Gateway:
                    SetCurControl(_gatewayControl);
                    break;
                case Constants.PageTag.NodeHistoryData:
                    SetCurControl(_nodeHistoryDataControl, data.Item2);
                    break;
            }
        }

        private void OnNodeHistoryDataSwitchPage(Tuple<string, object> data)
        {
            SetCurControl(_nodeControl, data?.Item2);
        }
    }
}
